package com.swt.get;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.google.common.io.CharStreams;
import com.swt.get.model.PhoneGroup;
import com.swt.get.model.PhoneGroups;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class Tst001 {

	private JFrame frame;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Tst001 window = new Tst001();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Tst001() {
		initialize();
	}

	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 649, 556);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 434, 10);
		frame.getContentPane().add(panel);
		// 클릭 이벤트
		JButton btnNewButton = clickSubmit();
		btnNewButton.setBounds(504, 236, 117, 30);
		frame.getContentPane().add(btnNewButton);

		
		urlField = new JTextField();
		urlField.setText("http://www.daum.net");
		urlField.setBounds(81, 31, 457, 21);
		frame.getContentPane().add(urlField);
		urlField.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(12, 286, 609, 222);
		frame.getContentPane().add(scrollPane);
		
		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		
		JLabel lblNewLabel_1 = new JLabel("URL");
		lblNewLabel_1.setBounds(12, 34, 526, 15);
		frame.getContentPane().add(lblNewLabel_1);
		
		
		
		JLabel lblRegex = new JLabel("Regex");
		lblRegex.setBounds(12, 65, 57, 15);
		frame.getContentPane().add(lblRegex);
		
		JCheckBox chckbxI = new JCheckBox("i");
		chckbxI.setBounds(83, 240, 45, 23);
		frame.getContentPane().add(chckbxI);
		
		JCheckBox chckbxB = new JCheckBox("b");
		chckbxB.setBounds(132, 240, 39, 23);
		frame.getContentPane().add(chckbxB);
		
		regexField = new JTextField();
		regexField.setText("([^\\\"]+\\\\.css)");
		regexField.setBounds(81, 62, 457, 21);
		frame.getContentPane().add(regexField);
		regexField.setColumns(10);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"html <img> : src", "html <css> : css"}));
		comboBox.setBounds(81, 97, 223, 21);
		frame.getContentPane().add(comboBox);
		
		JLabel lblNewLabel = new JLabel("Select");
		lblNewLabel.setBounds(12, 100, 57, 15);
		frame.getContentPane().add(lblNewLabel);
		
	}
	/**
	 * 클릭 이벤트
	 * @return
	 */
	private JButton clickSubmit() {
		JButton btnNewButton = new JButton("New button");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				regexField.getText();
				try {
					
					String resultHttp = fetch(urlField.getText());
					patternMatcher(resultHttp, regexField.getText());
//					textArea.setText(patternMatcher(resultHttp, regexField.getText()));
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		});
		return btnNewButton;
	}

	
	private PhoneGroups m_groups = new PhoneGroups();
	private JTextField urlField;
	private JTextArea textArea;
	private JTextField regexField;
	
	// TEXT AREA
	public JTextArea getTextArea() {
		return textArea;
	}
	
	// PATTERN
	public JTextField getRegExTextField() {
		return regexField;
	}
	
	// HTTP GET
	public String fetch(String url) throws IOException {
		
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse response = httpClient.execute(httpGet);
		
		HttpEntity entity = response.getEntity();
		String str = CharStreams.toString(new InputStreamReader(entity.getContent() , "UTF-8"));
		return str;
		
		
	}
	
	// 패턴 적용후 TEXTAREA
	public void patternMatcher(String getHttpResponse , String patternStr) {
		System.out.println("====================================");
		//System.out.println(getHttpResponse);
		
		Pattern p = Pattern.compile(patternStr, Pattern.MULTILINE);
		Matcher m = p.matcher(getHttpResponse);
		while(m.find()) {
			textArea.setText(m.group(1));
//			System.out.println(m.group(1));
		}
		
		
	}
}
